class ArrayUtils {
    /*
    	功能：数组去重;
    	@params(array) 传入需要去重的数组
    */
    SortArray(array) {
        let newArry = [];
        array.forEach((item, index) => {
            if (array.indexOf(item) == index) {
                newArry.push(item);
            }
        })
        return newArry;
    }
    /* 
       功能：金额小写转大写
       @params(n) 传入金额
    */
    TransformNumber(n) {
        var fraction = ['角', '分'];
        var digit = ['零', '壹', '贰', '叁', '肆', '伍', '陆', '柒', '捌', '玖'];
        var unit = [
            ['元', '万', '亿'],
            ['', '拾', '佰', '仟']
        ];
        var head = n < 0 ? '仟' : '';
        n = Math.abs(n);
        var s = '';
        for (var i = 0; i < fraction.length; i++) {
            s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, '');
        }
        s = s || '整';
        n = Math.floor(n);
        for (var i = 0; i < unit[0].length && n > 0; i++) {
            var p = '';
            for (var j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[n % 10] + unit[1][j] + p;
                n = Math.floor(n / 10);
            }
            s = p.replace(/(零.)*零$/, '').replace(/^$/, '零') + unit[0][i] + s;
        }
        return head + s.replace(/(零.)*零元/, '元').replace(/(零.)+/g, '零').replace(/^整$/, '零元整');
    }
}

class DateUtils {
    constructor(arg) {}
    /*
    	功能：时间戳转换为天、时、分、秒，并倒计时;
    	@params(timestamp) 传入需要转换的时间戳
    	使用方法：
    	setInterval(() => {
    		times = Groups.transTime(timestamp)
    		timestamp = timestamp - 1;
    	}, 1000)
    */
    TransTime(timestamp) {
        let result = {},
            $days, $hours, $minutes, $secend;
        if (timestamp >= 86400) {
            $days = Math.floor(timestamp / 86400);
            timestamp = timestamp % 86400;
            result.d = $days
            if (timestamp <= 0) {
                result.d = '';
            }
        }
        if (timestamp >= 3600) {
            $hours = Math.floor(timestamp / 3600);
            timestamp = timestamp % 3600;
            if ($hours < 10) {
                $hours = '0' + $hours;
            }
            result.h = $hours;

        }
        if (timestamp >= 60) {
            $minutes = Math.floor(timestamp / 60);
            timestamp = timestamp % 60;
            if ($minutes < 10) {
                $minutes = '0' + $minutes;
            }
            result.m = $minutes;
        }
        $secend = Math.floor(timestamp);
        if ($secend < 10) {
            $secend = '0' + $secend;
        }
        result.s = $secend;
        return result;
    }
}

class PathQuery {
    constructor(arg) {}
    /*
    	功能：请求地址参数转换为JSON对象;
    	@params(url) 传入需要转换的get地址
    */
    ParamJson(url) {
        let param = {}; // 存储最终JSON结果对象
        url.replace(/([^?&]+)=([^?&]+)/g, function(s, v, k) {
            param[v] = decodeURIComponent(k); //解析字符为中文
            return k + '=' + v;
        });
        return param;
    }
}

class StringUtils {
    /*
    	功能：返回随机验证码;
    */
    // 封装一个随机生成数字的函数
    random(a, b) {
        var n = Math.round(Math.random() * (a - b) + b);
        return n;
    }
    randomStr() {
        var strData = "";
        //如果觉得12个数太少也可以多放点，将i<4修改即可
        for (var i = 0; i < 4; i++) {
            var num = random(0, 9); //数字
            var upper = String.fromCharCode(random(65, 90)); //大写字母
            var lower = String.fromCharCode(random(97, 122)); //小写字母
            strData = strData + num + upper + lower; //将所有结果放进strData中

        }
        var str = "";
        for (var i = 0; i < 6; i++) {
            str += strData[random(0, strData.length - 1)]; //在strData里面随机抽取六个数
        }
        return str;

    }

}
let Groups = {
    name: '技术交流组',
    ArrayUtils,
    DateUtils,
    PathQuery
}
